home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows News 2010 Summer - Disc 1
/
WN_Ete2010_CD1.iso
/
Onglet5
/
Weezo
/
Weezo setup.exe
/
{code_appDir}
/
www
/
local
/
logOutputs
/
ftp.php
< prev
next >
Wrap
PHP Script
|
2010-05-19
|
5KB
|
143 lines
<?php
/**
* Update an HTML page on a personal website throuht FTP
* Asynchronously called by cfLogEvent not to block calling script if this script takes time to respond
* Basic presentation:
* - Retreive HTML template located into theme directory.
* (This template is copied from /www/local/logOutput if not existing, thus allowing user to modify template located into theme dir)
* - retreive previous events (such as server state or last connections / resources views) from /data/lastDisplayedFTPEvents.txt
* (this file is a serialized array :
* 'serverState'=>array(timestamp,logstring)
* 'events'>array(timestamp=>logstring, timestamp=>eventType/logstring, timestamp=>logstring, ...)
*
* - rebuild page mixing new event and previous ones
* - send to FTP server
*
*
* PHP version 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category NA
* @package NA
* @author Nicolas Bruley / Peer 2 World <contact@weezo.net>
* @copyright 2005-2009 Nicolas Bruley / Peer 2 World
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id:$
* @link http://www.weezo.net
* @since File available since Release 2.0.0
*/
ignore_user_abort(true);
if(!($logString=base64_decode(@$_GET['b64LogString']))) die();
// Get FTP configuration
$ftpConfig=@unserialize(cfStoredPasswordDecrypt(cfGGetVar('ftpEventConfig')));
if(!isset($ftpConfig['host']) || !isset($ftpConfig['user']) || !isset($ftpConfig['password']) || !isset($ftpConfig['path']) || !isset($ftpConfig['displayIP'])){
cfLog('FTP event logging aborted - incorrect FTP configuration',LOG_DEBUG);
exit;
}
// Retreive template
$templateFile=cfAppDocRoot().'/local/logOutputs/FTP template.html';
$userTemplateFile=cfAppDocRoot().'/themes/FTP template.html';
if(!file_exists($userTemplateFile)) copy($templateFile,$userTemplateFile);
$html=file_get_contents($userTemplateFile);
// Retreive current displayed page data
$lastDisplayedFTPEventsFile=cfAppDataDir().'/lastDisplayedFTPEvents.txt';
if(file_exists($lastDisplayedFTPEventsFile)) $lastDisplayedFTPEvents=@unserialize(file_get_contents($lastDisplayedFTPEventsFile));
/**
* Server state & time
*/
if($_GET['eventType']==EVENT_SERVERSTATE || !isset($lastDisplayedFTPEvents['serverState'])){
$regInfo=cfIsRegistered();
// Replace logstring as it's not correctly HTML formated
$logString=APPLICATION_NAME.' - '.cfCaption('apacheStartedWeb',false,false,false,true);
if(isset($regInfo['regName'])) $logString.='<a class="serverStateURL" href="'.DNS_SITE.'/'.$regInfo['regName'].'">'.DNS_SITE.'/'.$regInfo['regName'].'</a>';
if(@$ftpConfig['displayIP'])
$logString.=' <a class="serverStateIP" href="'.cfExternalHostName().'">('.cfCaption('genLink',false,false,false,true).')</a>';
$lastDisplayedFTPEvents['serverState']=array(time(),$logString);
$lastDisplayedFTPEvents['events']=array(); // Reset events
}
// Time
if($lastDisplayedFTPEvents['serverState'][0])
$html=str_replace('{serverTime}',date(cfCaption('_FULL_DATE_FORMAT'),$lastDisplayedFTPEvents['serverState'][0]),$html);
else
$html=str_replace('{serverTime}','',$html); // (shouldn't happend)
// State
$html=str_replace('{serverState}',$lastDisplayedFTPEvents['serverState'][1],$html);
/**
* Events
*/
if($_GET['eventType']!=EVENT_SERVERSTATE) $lastDisplayedFTPEvents['events'][time()]=((int)@$_GET['eventType']).'/'.$logString;
// Sort
krsort($lastDisplayedFTPEvents['events']);
// Generate HTML
$eventsHTML='';
foreach ($lastDisplayedFTPEvents['events'] as $time=>$logString){
@list($eventType,$logString)=explode('/',$logString,2);
if($eventType==EVENT_RESOURCEACCESS) $eventType='eventResourceAccess'; else $eventType='eventConnection';
$eventsHTML.='<div class="event">'."\n";
$eventsHTML.=' <span class="eventTime">'.date(cfCaption('_FULL_DATE_FORMAT'),$time).'</span>'."\n";
$eventsHTML.=' <span class="'.$eventType.'">'.$logString."</span>\n";
$eventsHTML.="</div>\n";
}
$html=str_replace('{events}',$eventsHTML,$html);
// Save events
file_put_contents($lastDisplayedFTPEventsFile,serialize($lastDisplayedFTPEvents));
/**
* FTP send
*/
// Setup FTP connection
if($ftpConfig['port']=='990') $connId = ftp_ssl_connect($ftpConfig['host'],990);
else $connId = ftp_connect($ftpConfig['host'],$ftpConfig['port']);
if(!$connId){
cfLog('FTP event logging aborted - could not establish FTP connection',LOG_DEBUG);
exit;
}
// Login with username and password
$loginResult = @ftp_login($connId, $ftpConfig['user'], $ftpConfig['password']);
if(!$loginResult){
cfLog('FTP event logging aborted - could not connect with given user/password',LOG_DEBUG);
exit;
}
// Put file
$tmpFile=cfAppTempDir().'/tmp_FTP.html'; file_put_contents($tmpFile,$html);
$result=ftp_put($connId, $ftpConfig['path'], $tmpFile, FTP_ASCII);
@unlink($tmpFile);
if(!$result){
cfLog('FTP event logging failed - could not put file on server',LOG_DEBUG);
exit;
}
// Close FTP connection
ftp_close($connId);
//@unlink('C:/P1/www/testZ.php'); cfDbg($html,'C:/P1/www/testZ.php',false);